package com.example.mac.yuejian14.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import java.util.List;

import com.example.mac.yuejian14.activity.SearchClubActivity;
import com.example.mac.yuejian14.listener.RetrofitListener1;
import com.example.mac.yuejian14.R;
import com.example.mac.yuejian14.adapter.ClubListAdapter;
import com.example.mac.yuejian14.bean.Club;
import com.example.mac.yuejian14.model.ClubModel;

public class ClubFragment extends Fragment implements RetrofitListener1 {
	private RecyclerView recyclerView;
	private ClubListAdapter clubListAdapter;
	private ImageButton search_club;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_club, container, false);
		initData();
		return initView(view);
	}



	private View initView(View view) {
		recyclerView = (RecyclerView) view.findViewById(R.id.home_recycler);
		search_club = (ImageButton) view.findViewById(R.id.btn_search_club);
		search_club.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), SearchClubActivity.class);
				startActivity(intent);
			}
		});

		return view;
	}

	private void initData() {
		ClubModel clubModel = new ClubModel();
		clubModel.getclublist(this);
	}

	@Override
	public void onSuccess(Object object, int flag) {
		if (getActivity() != null){
			List<Club> club = (List<Club>) object;
			clubListAdapter = new ClubListAdapter(getActivity(),club);
			LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
			layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
			recyclerView.setLayoutManager(layoutManager);
			recyclerView.setAdapter(clubListAdapter);
			recyclerView.setItemAnimator(new DefaultItemAnimator());
		}
	}

	@Override
	public void onFail() {

	}

}
